package com.yanqu.road.server.manger.maidian.beauty;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.entity.enums.eSkinType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserBeautySkill;
import com.yanqu.road.entity.player.UserSkin;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeautyLog {

    public static void trackBeautyInfoSnapshot(GamePlayer player){
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            //公共事件属性
            DataBuilder common = DataBuilder.newBuilder(player.getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
            DataAnalyticsMgr.buildPublicEventData(player, common);

            Map<Integer, UserBeauty> userBeautyMap = player.getModule(BeautyModule.class).getBeautyMap();
            List<UserSkin> userSkinList = player.getModule(SkinModule.class).getSkinList();
            for (Map.Entry<Integer, UserBeauty> beautyEntry : userBeautyMap.entrySet()) {
                UserBeauty userBeauty = beautyEntry.getValue();
                BeautyInfo beautyInfo = BeautyMgr.getBeautyInfo(userBeauty.getBeautyId());
                if(beautyInfo == null){
                    continue;
                }
                DataBuilder dataBuilder = DataBuilder.newBuilder(player.getUserInfo().getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_NAME, userBeauty.getBeautyId());
                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_QUALITY, userBeauty.getQuality());
                //美名等级
                dataBuilder.build(EventDataConstants.Beauty.BEAUTY_NAME, userBeauty.getIdentityLv());
                dataBuilder.build(EventDataConstants.Beauty.VOCATION, beautyInfo.getOccupation());

                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_LOVE, userBeauty.getTotalIntimacy());  //当前亲密
                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_FLOWER, userBeauty.getTotalGlamour()); //当前魅力

                //服装
                List<Map<String, Object>> objectGroup = new ArrayList<>();
                for (UserSkin userSkin : userSkinList) {
                    SkinInfo skinInfo = SkinMgr.getSkinInfo(userSkin.getSkinId());
                    if(skinInfo != null){
                        if(skinInfo.getBelongRole() != userBeauty.getBeautyId()){
                            continue;
                        }
                        Map<String, Object> objectMap = new HashMap<>();
                        objectMap.put(EventDataConstants.Beauty.INTIMATE_CLOTHINGID, userSkin.getSkinId());
                        objectMap.put(EventDataConstants.Beauty.INTIMATE_CLOTHINGLV, userSkin.getLevel());
                        objectGroup.add(objectMap);
                    }
                }
                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_CLOTHING, objectGroup);

                //商铺技能
                int totalNum = 0;
                int totalMaxLvNum = 0;
                for (Map.Entry<Integer, UserBeautySkill> beautySkillEntry : userBeauty.getSkillMap().entrySet()) {
                    UserBeautySkill userBeautySkill = beautySkillEntry.getValue();
                    totalNum++;
                    if(userBeautySkill.getSkillValue() >= 30){
                        totalMaxLvNum++;
                    }
                }
                List<Integer> buildSkillList = new ArrayList<>();
                buildSkillList.add(totalNum);
                buildSkillList.add(totalMaxLvNum);
                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_SHOPSKILL, buildSkillList);

                //门客技能
                //缘分、才艺
                List<Map<String, Object>> patronsSkillGroup = new ArrayList<>();
                List<Integer> skillList = StringUtils.stringToIntegerList(GameConfig.BEAUTY_PATRONS_SKILL, "\\|");
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLTYPE, 1);
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLNAME, skillList.get(0));
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLLV, userBeauty.getSkill1());
                patronsSkillGroup.add(objectMap);

                objectMap = new HashMap<>();
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLTYPE, 1);
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLNAME, skillList.get(1));
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLLV, userBeauty.getSkill2());
                patronsSkillGroup.add(objectMap);

                List<Integer> escortSkillList = StringUtils.stringToIntegerList(GameConfig.BEAUTY_ESCORT_PATRONS_SKILL, "\\|");
                objectMap = new HashMap<>();
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLTYPE, 2);
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLNAME, escortSkillList.get(0));
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLLV, userBeauty.getEscortSkill1());
                patronsSkillGroup.add(objectMap);

                objectMap = new HashMap<>();
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLTYPE, 2);
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLNAME, escortSkillList.get(1));
                objectMap.put(EventDataConstants.Beauty.INTIMATE_GUESTSKILLLV, userBeauty.getEscortSkill2());
                patronsSkillGroup.add(objectMap);

                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_GUESTSKILL, patronsSkillGroup);

                //仪容技能
                List<Map<String, Object>> skinSkillGroup = new ArrayList<>();
                objectMap = new HashMap<>();

                int maxSkillLv = 0;
                SkillInfo skinSkillInfo = SkillMgr.getSkillInfo(beautyInfo.getHaloSkill(0));
                if(skinSkillInfo != null && skinSkillInfo.getUpgradeParamList().size() > 0){
                    SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfoIfNotGetMax(skinSkillInfo.getUpgradeParamList().get(0), userBeauty.getSkinSkill2());
                    if (null != skillUpgradeInfo) {
                        maxSkillLv = skillUpgradeInfo.getLevel();
                    }
                }
                Map<Integer, UserSkin> skinMap = player.getModule(SkinModule.class).getUserSkinMap();
                for(UserSkin userSkin : skinMap.values()){
                    SkinInfo skinInfo = SkinMgr.getSkinInfo(userSkin.getSkinId());
                    if(skinInfo.getType() == eSkinType.Beauty.getValue() && skinInfo.getBelongRole() == userBeauty.getBeautyId()){
                        int skillUpgradeType = skinInfo.getSkinBeautySkillUpgradeType(0);
                        SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfoIfNotGetMax(skillUpgradeType, userSkin.getLevel());
                        if (null != skillUpgradeInfo) {
                            maxSkillLv += skillUpgradeInfo.getConsumeCount();
                        }
                    }
                }

                objectMap.put(EventDataConstants.Beauty.INTIMATE_LOOKSKILLNAME, beautyInfo.getHaloSkill(0));
                objectMap.put(EventDataConstants.Beauty.INTIMATE_LOOKSKILLMAX, maxSkillLv);
                objectMap.put(EventDataConstants.Beauty.INTIMATE_LOOKSKILLLV, userBeauty.getSkinSkill1());
                skinSkillGroup.add(objectMap);


                maxSkillLv = 0;
                skinSkillInfo = SkillMgr.getSkillInfo(beautyInfo.getHaloSkill(1));
                if(skinSkillInfo != null && skinSkillInfo.getUpgradeParamList().size() > 0){
                    SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfoIfNotGetMax(skinSkillInfo.getUpgradeParamList().get(0), userBeauty.getSkinSkill2());
                    if (null != skillUpgradeInfo) {
                        maxSkillLv = skillUpgradeInfo.getLevel();
                    }
                }

                for(UserSkin userSkin : skinMap.values()){
                    SkinInfo skinInfo = SkinMgr.getSkinInfo(userSkin.getSkinId());
                    if(skinInfo.getType() == eSkinType.Beauty.getValue() && skinInfo.getBelongRole() == userBeauty.getBeautyId()){
                        int skillUpgradeType = skinInfo.getSkinBeautySkillUpgradeType(1);
                        SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfoIfNotGetMax(skillUpgradeType, userSkin.getLevel());
                        if (null != skillUpgradeInfo) {
                            maxSkillLv += skillUpgradeInfo.getConsumeCount();
                        }
                    }
                }

                objectMap = new HashMap<>();
                objectMap.put(EventDataConstants.Beauty.INTIMATE_LOOKSKILLNAME, beautyInfo.getHaloSkill(1));
                objectMap.put(EventDataConstants.Beauty.INTIMATE_LOOKSKILLMAX, maxSkillLv);
                objectMap.put(EventDataConstants.Beauty.INTIMATE_LOOKSKILLLV, userBeauty.getSkinSkill2());
                skinSkillGroup.add(objectMap);

                dataBuilder.build(EventDataConstants.Beauty.INTIMATE_LOOKSKILL, skinSkillGroup);
                dataBuilder.getDataMap().putAll(common.getDataMap());
                DataAnalyticsMgr.provider.track(dataBuilder.getAccountId(), dataBuilder.getDistinctId(), EventDataConstants.Beauty.INTIMATE_INFO, dataBuilder.getDataMap());
            }

        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error("error :", e);
        }
    }

    public static void trackBeautyMusicRoomSkillUp(GamePlayer player, int beautyId, int skillId, int newLevel) {
        DefaultEventTraceAction action = new DefaultEventTraceAction("music_skillup", player);
        action.build("intimate_name", beautyId);
        action.build("intimate_skillname", skillId);
        action.build("intimate_afterlevel", newLevel);
        action.trace();
    }
}
